package com.service;

import com.dao.PurchaseJdbc;
import com.model.Purchase;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class PurchaseService {
    PurchaseJdbc pj2 = new PurchaseJdbc();
    public List<Purchase> show() {
        List<Purchase> list = new ArrayList<Purchase>();
        ResultSet rs = pj2.show();
        try {
            while (rs.next()){
                int oid = rs.getInt("id");
                String ticket = rs.getString("ticket");
                String realname = rs.getString("realname");
                String splace = rs.getString("splace");
                String splaces = rs.getString("splaces");
                Date stime = rs.getDate("stime");
                Date etime = rs.getDate("etime");
                String lev = rs.getString("lev");
                String price = rs.getString("price");
                String seat = rs.getString("seat");
                Purchase purchase = new Purchase(oid,ticket,realname,splace,splaces,stime,etime,lev,price,seat);
                list.add(purchase);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public void delete(String id) {
        pj2.delete(id);
    }

    public Purchase selectById(int id) throws SQLException {
        ResultSet rs = pj2.selectById(id);
        Purchase purchase =null;
        while(rs.next()){
            String ticket = rs.getString("ticket");
            String realname = rs.getString("realname");
            String lev = rs.getString("lev");
            String splace = rs.getString("splace");
            String splaces = rs.getString("splaces");
            Date stime = rs.getDate("stime");
            Date etime = rs.getDate("etime");
            String price = rs.getString("price");
            String seat = rs.getString("seat");
            purchase = new Purchase(rs.getInt("id"),realname,ticket,lev,splace,stime,etime,price,seat,splaces);
        }

        return purchase;
    }
//    public Teacher toupdate(String id) throws SQLException {
//        ResultSet rs = pj2.show();
//        Teacher teacher = null;
//        while (rs.next()) {
//            String tname = rs.getString("tname");
//            int tage = rs.getInt("tage");
//            String tsex = rs.getString("tsex");
//            teacher = new Teacher(rs.getInt("tid"), tname, tage, tsex);
//        }
//        return teacher;
//    }
//    public void update(String tid, String tname, String tage, String tsex) {
//        tj.update(tid,tname,tage,tsex);
//    }
}
